// 订单商品
var express = require('express');
var router = express.Router();
const orderGoodsService = require('../service/orderGoodsService');

//增加匹配 
router.post('/', async function (req, res) {

    // 自动生成当前的时间戳
    function getLastDate() {
        var date = new Date();
        return Date.parse(date)/1000;
    }

    req.body.time = getLastDate();
    console.log(req.body);
    let data = await orderGoodsService.insert(req.body)
    res.send(data);
})



//删除匹配
router.delete('/:id', async function (req, res) {
    let data = await orderGoodsService.delete(req.params.id)
    res.send(data);
})
//修改匹配
router.put('/:id', async function (req, res) {


    // 当修改的数据只是预约时间时   ， req.body.orderTime 格式必须为 2021-06-19 22:33:00
    if (req.body.orderTime) {
        // 获取预约时间戳
        let orderDate = new Date(req.body.orderTime);
        function orderTime(orderDate) {
            console.log("预约时间", orderDate);
            return Date.parse(orderDate) / 1000;
        }
        req.body.orderTime = orderTime(orderDate);
    }


    let data = await orderGoodsService.update(req.params.id, req.body)
    res.send(data);
})




//分页所有/条件查询
router.get('/all', async function (req, res) {
    let { type, value, current, size } = req.query;
    // console.log("type, value", type, value);
    let searchObj = {};
    if (type) { 
        searchObj[type] = { $regex: value }
     } else if(value) {
        searchObj['adminId'] = value
    }else{
        searchObj = {};
    }  // 使用正则可以实现模糊查询
    // console.log("这里",searchObj,req.query)
    let data = await orderGoodsService.findNo(searchObj, { current: current || 1, size: size || 5 })
    res.send(data);
})

//删除匹配s
router.get('/find/:id', async function (req, res) {
    console.log(req.params.id);
    let data = await orderGoodsService.findById(req.params.id)
    res.send(data);
})

module.exports = router;









